package com.aia.ilp.web.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.aia.ilp.domain.Cust;
import com.aia.ilp.domain.CustExample;
import com.aia.ilp.domain.CustExample.Criteria;
import com.aia.ilp.service.MainService;
import com.cfh.ahead.Constant;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustJson extends ActionSupport implements ModelDriven {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CustJson.class);

	private static final long serialVersionUID = 1L;
	private MainService mainService;
	private String SEARCH_MAIL;
	private Map root = new HashMap();
	private Cust cust = new Cust();
	private String[] idsToDel = new String[200];

	public CustJson() {
		super();

	}
	private int start;
	private int limit;
	private String sort;
	private String dir;

	/***
	 * 得到所有用户
	 */
	public String list() {
		CustExample example = new CustExample();
		Criteria crit = example.createCriteria();
		example.setOrderByClause(buildOrderString());
		if (Tools.hasText(SEARCH_MAIL)) {
			logger.debug("list()"); //$NON-NLS-1$
			crit.andCUST_MAILLike("%" + SEARCH_MAIL + "%");
		}
		//		
		// if(Tools.hasText(SERACH_CODE)){
		// logger.debug("SERACH_CODE="+SERACH_CODE);
		// crit.andPRD_CODELike("%"+SERACH_CODE+"%");
		// }
		// add more search

		List list = mainService.selectCust(example, start, limit);
		filter(list);
		int count = 0;
		try {
			count = mainService.countCust(example);
		} catch (Exception e) {
			count = Constant.MAX_COUNT_WHEN_EXCEPTION;
			logger.warn("list()", e); //$NON-NLS-1$
		}
		root.put("total", new Integer(count));
		root.put("items", list);
		return SUCCESS;
	}

	private void filter(List list) {
		try {
			for (Object o : list) {
				if (o != null) {
					Cust c = (Cust) o;
					c.setCUST_PASSWORD(null);
					c.setCUST_SECURITY_Q(null);
					c.setCUST_SECURITY_A(null);
				}
			}
		} catch (Exception e) {
			logger.error("filter(List) - end", e); //$NON-NLS-1$
			return;
		}

	}

	public String add() {
		logger.debug("add() - add() start"); //$NON-NLS-1$
		// try {
		// Assert.isTrue(Product != null);
		// try {
		// Product.setPRD_TYPE(Product.getPRD_TYPE().toUpperCase());
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		//			
		// productService.insert(Product);
		// root.put("success", new Boolean(true));
		// logger.debug("add() end");
		// return SUCCESS;
		// } catch (Exception e) {
		// e.printStackTrace();
		// throw new RuntimeException(e);
		// }

		return SUCCESS;
	}

	public String modify() {
		logger.debug("modify() - modify() start"); //$NON-NLS-1$
		// Assert.notNull(Product);
		//	
		// int i = productService.update(Product);
		// if (i > 0) {
		// root.put("success", new Boolean(true));
		// } else {
		// root.put("success", new Boolean(false));
		// }
		// logger.debug("modify() end");
		// return SUCCESS;

		return SUCCESS;
	}

	public String del() {
		logger.debug("del() - del() start"); //$NON-NLS-1$
		int count = 0;
		String String = null;
		for (int i = 0; i < idsToDel.length; i++) {
			if (idsToDel[i] != null) {
				int countcicle = mainService.deleteCustByPrimaryKey(idsToDel[i]);
				count += countcicle;
			}
		}
		root.put("success", new Boolean(true));
		root.put("count", new Integer(count));
		logger.debug("del() - del() end"); //$NON-NLS-1$
		return SUCCESS;
	}

	public String delpic() {
		// logger.debug("delpic() start");
		// int count = 0;
		// String String = null;
		// for (int i = 0; i < idsToDel.length; i++) {
		// if (idsToDel[i] != null) {
		// int countcicle = mainService.deleteCustPic(idsToDel[i]);
		// count +=countcicle;
		// }
		// }
		// root.put("success", new Boolean(true));
		// root.put("count", new Integer(count));
		// logger.debug("delpic() end");
		return SUCCESS;
	}

	private String buildOrderString() {
		String toReturn = null;
		if (Tools.hasText(sort)) {
			toReturn = sort;
			if (Tools.hasText(dir)) {
				toReturn += " " + dir;
			}
		}
		return toReturn;
	}

	/***
	 * nouse
	 */
	public String execute() {

		return SUCCESS;
	}

	public Map getRoot() {
		return root;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSort() {
		return sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getDir() {
		return dir;
	}

	public Object getModel() {
		return cust;
	}

	public void setIdsToDel(String[] idsToDel) {
		this.idsToDel = idsToDel;
	}

	public String[] getIdsToDel() {
		return idsToDel;
	}

	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

	public MainService getMainService() {
		return mainService;
	}

	public void setSEARCH_MAIL(String sEARCH_MAIL) {
		SEARCH_MAIL = sEARCH_MAIL;
	}

	public String getSEARCH_MAIL() {
		return SEARCH_MAIL;
	}

}
